package com.example.seh.home.substitution;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.seh.R;

import java.util.List;

public class CourierListAdapter extends BaseAdapter {
    private Context mContext;
    private List<DeliverDTO> couriers;
    private int item;

    public CourierListAdapter(Context mContext, List<DeliverDTO> couriers, int item) {
        this.mContext = mContext;
        this.couriers = couriers;
        this.item = item;
    }

    @Override
    public int getCount() {
        if (couriers != null) {
            return couriers.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (couriers != null) {
            return couriers.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.courierAddress = convertView.findViewById(R.id.courier_address);
            viewHolder.myAddress = convertView.findViewById(R.id.my_address);
            viewHolder.name = convertView.findViewById(R.id.my_name);
            viewHolder.phone = convertView.findViewById(R.id.my_phone);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.courierAddress.setText(couriers.get(position).getAddressName());
        viewHolder.myAddress.setText(couriers.get(position).getReceiveName());
        viewHolder.name.setText(couriers.get(position).getUserDTO().getUsername());
        viewHolder.phone.setText(couriers.get(position).getUserDTO().getPhone());
        return convertView;
    }

    class ViewHolder{
        private TextView courierAddress, myAddress, name, phone;
    }
}
